package com.atguigu.day05;

import com.atguigu.bean.Event;
import com.atguigu.day03.Flink05_Source_Custom;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.functions.windowing.WindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.util.HashSet;

public class Flink06_ProcessTimeWindow_WindowFun {
    public static void main(String[] args) throws Exception {
        //1.获取流的执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        //2.通过自定义数据源获取数据
        DataStreamSource<Event> streamSource = env.addSource(new Flink05_Source_Custom.ClickSource());

        //3.将数据聚合达到同一个分区
        KeyedStream<Event, String> keyedStream = streamSource.keyBy(new KeySelector<Event, String>() {
            @Override
            public String getKey(Event value) throws Exception {
                return "key";
            }
        });

        //4.开启一个基于处理时间的滚动窗口，每五秒计算一次5秒内的UV
        WindowedStream<Event, String, TimeWindow> window = keyedStream.window(TumblingProcessingTimeWindows.of(Time.seconds(5)));

        //TODO 5.使用全窗口函数WindowFun实现UV的计算
        window.apply(new WindowFunction<Event, Integer, String, TimeWindow>() {
            @Override
            public void apply(String s, TimeWindow window, Iterable<Event> input, Collector<Integer> out) throws Exception {
                System.out.println("apply......");
                //1.创建HashSet里面存放user，利用hashSet的幂等性去重，以此可以得到UV
                HashSet<String> userSet = new HashSet<>();
                //2.遍历迭代器 （注意：对于迭代器来说，只能使用一次，因此迭代器底层是链表，当遍历完毕之后，头指针会指向尾结点，因此没办法重复使用，如果想重复使用迭代器中的数据，可以将迭代器中的数据遍历出来放到集合中）
                for (Event element : input) {
                    userSet.add(element.user);
                }

                //3.输出uv结果
                out.collect(userSet.size());
            }
        }).print();

        env.execute();
    }
}
